package com.maila.xiaoaodispatch.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.maila.xiaoaodispatch.R;
import com.maila.xiaoaodispatch.api.DispatchApi;
import com.maila.xiaoaodispatch.net.VolleyInterface;
import com.maila.xiaoaodispatch.utils.RegularUtils;
import com.maila.xiaoaodispatch.utils.StartActivityUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 配送员登录
 * Created by mark on 2017/10/26.
 */

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.login_header)
    AutoLinearLayout loginHeader;
    @BindView(R.id.tv_zhanghao)
    TextView tvZhanghao;
    @BindView(R.id.please_enter_your_mobile_phone_et)
    EditText pleaseEnterYourMobilePhoneEt;
    @BindView(R.id.please_input_a_password)
    EditText pleaseInputAPassword;
    @BindView(R.id.all_denglu)
    AutoLinearLayout allDenglu;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.register_tv)
    TextView registerTv;
    @BindView(R.id.forget_password_tv)
    TextView forgetPasswordTv;
    boolean pwdOK=false;
    boolean phoneOK=false;
    String phone="";
    String pwd="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initlistener();


    }

    private void initlistener() {
        pleaseEnterYourMobilePhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(pleaseEnterYourMobilePhoneEt.getText()!=null&& RegularUtils.isPhoneNumber(pleaseEnterYourMobilePhoneEt.getText().toString())){
                    phoneOK=true;
                    phone=pleaseEnterYourMobilePhoneEt.getText().toString();
                }else {
                    phoneOK=false;
                }
                updataBtn();
            }
        });

        pleaseInputAPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(pleaseInputAPassword.getText()!=null&&pleaseInputAPassword.getText().length()>5){
                    pwdOK=true;
                    pwd=pleaseInputAPassword.getText().toString();
                }else {
                    pwdOK=false;
                }
                updataBtn();
            }


        });
    }
    private void updataBtn() {
        if(pwdOK&&phoneOK){
            btnLogin.setClickable(true);
            btnLogin.setBackgroundResource(R.drawable.bg_rd_orange);
        }else {
            btnLogin.setClickable(false);
            btnLogin.setBackgroundResource(R.drawable.bg_rd_gray);
        }

    }
    @OnClick({R.id.white_return_iv, R.id.btn_login, R.id.register_tv, R.id.forget_password_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.btn_login:
                StartActivityUtils.start(LoginActivity.this,MainActivity.class);
                new DispatchApi(this, new VolleyInterface() {
                    @Override
                    public void onSuccess(String result) {
                        try {
                            JSONObject jsonObject=new JSONObject(result);
                            if(jsonObject.optInt("result")==1){

                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, ""+jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                }).login(phone,pwd);

                break;
            case R.id.register_tv:
                StartActivityUtils.start(this,RegisterActivity.class);
                break;
            case R.id.forget_password_tv:
                StartActivityUtils.start(this,ForgetPwdActivity.class);
                break;
        }
    }
}
